﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using System.Web.Routing;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Concrete;


namespace SimpleTaskManagerUpdated.Infrastructure
{
    /// <summary>
    /// Class for injecting the implementation of interfaces where they are needed
    /// </summary>
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        /// <summary>
        /// A default function needed for injection
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controllerType"></param>
        /// <returns></returns>
        protected override IController GetControllerInstance(RequestContext requestContext,
        Type controllerType)
        {
            return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
        }

        /// <summary>
        /// The functions where the bindings between interfaces and their implementation takes place
        /// </summary>
        private void AddBindings()
        {
            ninjectKernel.Bind<IUserRepository>().To<EFUserRepository>();
        }
    }
}